﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TimeSolve
{
    /// <summary>
    /// HelixGraphControl.xaml 的交互逻辑
    /// </summary>
    public partial class DGraphControl : UserControl
    {
        public DGraphControl()
        {
            InitializeComponent();
            viewControler = new ViewControler(vcoordinate);

            vGraphGrid.MouseDown += vMouseGrid_MouseDown;
            vGraphGrid.MouseMove += vMouseGrid_MouseMove;
            vGraphGrid.MouseWheel += vMouseGrid_MouseWheel;
            vGraphGrid.MouseUp += vMouseGrid_MouseUp;
            vGraphGrid.MouseLeave += vMouseGrid_MouseLeave;

            defaultViewport = new Viewport3D();
            defaultViewport.Camera = new OrthographicCamera()
            {
                Position = new Point3D(0, 0, 10),
                LookDirection = new Vector3D(0, 0, -1),
                UpDirection = new Vector3D(0, 1, 0),
                Width = 10
            };
            loadDefaultViewport();
        }

        Viewport3D defaultViewport { get; set; }
        public object BindGraphItem
        {
            get { return (object)GetValue(BindGraphItemProperty); }
            set { SetValue(BindGraphItemProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BindGraphItem.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BindGraphItemProperty =
            DependencyProperty.Register("BindGraphItem", typeof(object), typeof(DGraphControl), new PropertyMetadata(null,new PropertyChangedCallback(BindGraphItemChangedCallBack)));
        public static void BindGraphItemChangedCallBack(DependencyObject d,DependencyPropertyChangedEventArgs e)
        {
            DGraphControl gc = d as DGraphControl;
            if(e.NewValue is MonitorItem) 
            {
                gc.Visibility = Visibility.Collapsed; 
            }else
            {
                gc.Visibility = Visibility.Visible;
            }
            GraphItem gi = e.NewValue as GraphItem;
            if (gi != null)
            {
                gc.vGraphContent.Content = gi.viewport3D;
                gc.viewControler.linkViewport(gi.viewport3D , gi.light as DirectionalLight, gi.graphCenter, gi.graphRadius);
                gc.vGraphBar.Content = gi.colorBar;
            }
            else
            {
                if(e.OldValue is GraphItem)
                {
                    gc.loadDefaultViewport();
                }
            }  
        }
        private void loadDefaultViewport()
        {
            vGraphContent.Content = defaultViewport;
            viewControler.linkViewport(defaultViewport, null, new Point3D(0, 0, 0), 10);
        }

        public ViewControler viewControler { get; set; }


        private void changeMouseEvent()//暂时不用，后续用于更改鼠标操作
        {

        }
        private void ButtonFit_Click(object sender, RoutedEventArgs e)
        {
            viewControler.fit();
        }
        private void ButtonTest_Click(object sender, RoutedEventArgs e)
        {
            AxisAngleRotation3D r1 = new AxisAngleRotation3D(new Vector3D(0, 0, 1), 45);
            Point3D p = new Point3D(2, 2, 2);
            Vector3D v = new Vector3D(2, 2, 2);

            Matrix3D m = new Matrix3D();
            m.Rotate(new Quaternion(new Vector3D(0, 0, 1), 45));
            Vector3D v2 = m.Transform(v);
            Console.WriteLine(v2);

        }
        private void ButtonSetView_Click(object sender, RoutedEventArgs e)
        {
            Window w = new SetView(this);
            w.Owner = MainViewModel.mainViewModel.mainWindow;
            w.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            w.Show();
        }
        private void ButtonBigger_Click(object sender, RoutedEventArgs e)
        {
            viewControler.zoom(1.2, 0, 0);
        }
        private void ButtonSmaller_Click(object sender, RoutedEventArgs e)
        {
            viewControler.zoom(0.8, 0, 0);
        }
        //mousedown 任意按键
        private void vMouseGrid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = true;
        }
        private bool isMouseDown = false;//在标题栏双击，可能导致触发mousemove 的实践，加一变量判断是否是在grid里单机
        private Point mousePosLast;
        private void vMouseGrid_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Cursor = Cursors.ScrollAll;
                    Point mousePosNow = e.GetPosition(vGraphGrid);
                    viewControler.rotate((mousePosNow.Y - mousePosLast.Y) / vGraphGrid.ActualWidth, (mousePosNow.X - mousePosLast.X) / vGraphGrid.ActualWidth);
                    // cameraTrans.RotateCamera((PointMouseNow.Y - positionMouseDown.Y) / vGrid3D.ActualHeight * 2, (PointMouseNow.X - positionMouseDown.X) / vGrid3D.ActualHeight * 2);
                }
                else if (e.MiddleButton == MouseButtonState.Pressed)
                {
                    Cursor = Cursors.SizeAll;
                    Point PointMouseNow = e.GetPosition(vGraphGrid);
                    viewControler.move((PointMouseNow.Y - mousePosLast.Y) / vGraphGrid.ActualWidth, (PointMouseNow.X - mousePosLast.X) / vGraphGrid.ActualWidth);
                }
                else
                {
                    //  if (Cursor != Cursors.Arrow) { Cursor = Cursors.Arrow; }
                    Cursor = Cursors.Arrow;
                    isMouseDown = false;
                }
            }
            mousePosLast = e.GetPosition(vGraphGrid);
        }
        private void vMouseGrid_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Point mp = e.GetPosition(vGraphGrid);
            if (e.Delta > 0)
            {
                viewControler.zoom(1.05, mp.X / vGraphGrid.ActualWidth - 0.5, -(mp.Y - vGraphGrid.ActualHeight / 2) / vGraphGrid.ActualWidth);
            }
            else if (e.Delta < 0)
            {
                viewControler.zoom(0.95, 0,0);
            }
        }
        private void vMouseGrid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Cursor = Cursors.Arrow;
            isMouseDown = false;
        }

        private void vMouseGrid_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
            isMouseDown = false;
        }
    }
}




//public GraphItem GraphObject
//{
//    get { return (GraphItem)GetValue(GraphObjectProperty); }
//    set { SetValue(GraphObjectProperty, value); }
//}
//public static readonly DependencyProperty GraphObjectProperty =
//    DependencyProperty.Register("GraphObject", typeof(GraphItem), typeof(HelixGraphControl), new PropertyMetadata(null,new PropertyChangedCallback (GraphObjectChanged)));

//public static void GraphObjectChanged(DependencyObject obj,DependencyPropertyChangedEventArgs e)
//{
//    if (e == null) return;
//    HelixGraphControl hc = obj as HelixGraphControl;
//    GraphItem  graphData = e.NewValue as GraphItem;
//    hc.vhelix.Children.Clear();
//    if (graphData == null)
//    {
//        //hc.helixVisual3Ds = null;

//    }else
//    {
//        List<ModelVisual3D> ml = graphData.getVisual3Ds();
//        foreach (ModelVisual3D m in ml)
//        {
//            hc.vhelix.Children.Add(m);
//        }
//       // hc.helixVisual3Ds = graphData.helixVisual3Ds;
//    }
//}